package com.project.weather.controller;

import com.project.weather.aspectj.Log;
import com.project.weather.common.AResultCode;
import com.project.weather.common.Constant;
import com.project.weather.common.ResultMap;
import com.project.weather.enums.BusinessType;
import com.project.weather.pojo.UserBean;
import com.project.weather.service.UserRoleService;
import com.project.weather.service.UserService;
import com.project.weather.utils.RSAEncrypt;
import com.project.weather.utils.ServletUtils;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 *<p>Title:UserController.java</p>
 *<p>Description:登录 Action</p>
 *@author xxx
 *@version 1.0
 */
@RequestMapping(name="账号相关", value="/account/")
@RestController
public class LoginController {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;

    @Log(title = "普通用户登录", businessType = BusinessType.LOGIN)
    @RequestMapping(name="用户登录",value="userLogin",method = RequestMethod.POST)
    public ResultMap<AResultCode, UserBean> userLogin(@ApiParam(value = "对象数据", required = true) @RequestBody UserBean bean,HttpServletRequest request){
        ResultMap<AResultCode, UserBean> result = new ResultMap<>();


        ResultMap<AResultCode, UserBean> byUserName = userService.getByUserName(bean.getUsername());

        if(AResultCode.isFailed(byUserName)){
            result.setCode(AResultCode.WRONG_USERNAME);
            return result;
        }


        UserBean userBean = byUserName.getResult();

        if("0".equals(userBean.getStatus())){
            result.setCode(AResultCode.WRONG_FORBIDDEN);
            return result;
        }

        String decrypt = null;
        try {
            decrypt = RSAEncrypt.decrypt(userBean.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(bean.getPassword().equals(decrypt)){
            //登录成功
            ServletUtils.getSession().setAttribute(Constant.CURRENT_ACCOUNT,userBean);
//            request.getSession().setAttribute(Constant.CURRENT_ACCOUNT,userBean);
//            ServletUtils.getSession().setAttribute(Constant.CURRENT_ACCOUNT,userBean);
            result.setCode(AResultCode.OK);
            result.setResult(userBean);
        }else{
            result.setCode(AResultCode.LOGINERROR);
            return result;
        }

        return result;
    }

    @Log(title = "管理员登录", businessType = BusinessType.LOGIN)
    @RequestMapping(name="管理员登录",value="adminLogin",method = RequestMethod.POST)
    public ResultMap<AResultCode, UserBean> adminLogin(@ApiParam(value = "对象数据", required = true) @RequestBody UserBean bean, HttpServletRequest request){
        ResultMap<AResultCode, UserBean> result = new ResultMap<>();


        ResultMap<AResultCode, UserBean> byUserName = userService.getByUserName(bean.getUsername());

        if(AResultCode.isFailed(byUserName)){
            result.setCode(AResultCode.WRONG_USERNAME);
            return result;
        }


        UserBean userBean = byUserName.getResult();


        if("0".equals(userBean.getStatus())){
            result.setCode(AResultCode.WRONG_FORBIDDEN);
            return result;
        }

        String role_name = userBean.getRole_name();

        if("user".equals(role_name)){
            result.setCode(AResultCode.WRONG_USERNAME);
            return result;
        }

        String decrypt = null;
        try {
            decrypt = RSAEncrypt.decrypt(userBean.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(bean.getPassword().equals(decrypt)){
            //登录成功
            ServletUtils.getSession().setAttribute(Constant.CURRENT_ACCOUNT,userBean);
//            request.getSession().setAttribute(Constant.CURRENT_ACCOUNT,userBean);
//            ServletUtils.getSession().setAttribute(Constant.CURRENT_ACCOUNT,userBean);
            result.setCode(AResultCode.OK);
            result.setResult(userBean);
        }else{
            result.setCode(AResultCode.LOGINERROR);
            return result;
        }

        return result;
    }



    @Log(title = "用户注册", businessType = BusinessType.REGISTER)
    @RequestMapping(name="用户注册",value="userRegister",method = RequestMethod.POST)
    public ResultMap<AResultCode, UserBean> userRegister(@ApiParam(value = "对象数据", required = true) @RequestBody UserBean bean){
        ResultMap<AResultCode, UserBean> result = new ResultMap<>();

        userService.insert(bean);

        result.setCode(AResultCode.OK);
        return result;
    }

    @Log(title = "注销", businessType = BusinessType.LOGOUT)
    @RequestMapping(name="用户登出",value="userLogout",method = RequestMethod.POST)
    public ResultMap<AResultCode, UserBean> userLogout(){
        ResultMap<AResultCode, UserBean> result = new ResultMap<>();

        ServletUtils.getSession().removeAttribute(Constant.CURRENT_ACCOUNT);

        result.setCode(AResultCode.OK);
        return result;
    }

    @RequestMapping(value="error401",method = RequestMethod.GET)
    public ResultMap<AResultCode, Object> error401(){
        ResultMap<AResultCode, Object> result = new ResultMap<>();

        result.setCode(AResultCode.NOTAUTH);
        return result;
    }


}
